import java.awt.Color;
import java.util.ArrayList;

public class DataSample implements IDataSampleable {
	protected Matrix matrix;
	protected ArrayList<Double> mins;
	protected ArrayList<Double> maxs;
	protected ColorMap map;
	protected ArrayList<String> headers = null;
		
	public DataSample()
	{
		map = new ColorMap();		
	}
	
	public void loadData(IMatrixLoader loader)
	{
		if(matrix != null)
			matrix = null;
		
		matrix = loader.load();
		
		headers = loader.getHeaders();
		
		CalculateRangeValues();
		
		Correlation correlation = Correlation.GetInstance();
		
		correlation.doCorrelation(matrix);
		
		matrix.sort();
	}
	
	protected void CalculateRangeValues()
	{
		if(mins != null)
			mins.clear();			
		mins = new ArrayList<Double>(matrix.getColumnSize());
		if(maxs != null)
			maxs.clear();
		maxs = new ArrayList<Double>(matrix.getColumnSize());
			
		for(int j = 0; j < matrix.getColumnSize(); j++)
		{
			mins.add(matrix.getMinColumValue(j));
			maxs.add(matrix.getMaxColumValue(j));
		}
	}

	public int getColumnSize() {

		return matrix.getColumnSize();
	}


	public int getRowSize() {

		return matrix.getRowSize();
	}
	
	public Color getColor(int rowIndex, int columnIndex) {		
		map.setCMin(mins.get(columnIndex).floatValue());
		map.setCMax(maxs.get(columnIndex).floatValue());
		return map.getColor(matrix.getValue(rowIndex, columnIndex));
	}

	@Override
	public ArrayList<String> getHeaders() {
		return headers;
	}

}
